<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // 在数组原型上添加 myForEach 方法
      Array.prototype.myForEach = function (callback) {
        for (let i = 0; i < this.length; i++) {
          callback(this[i], i, this)
        }
      }
      let arr = [1, 2, 3, 4] //全局arr
      arr.myForEach((item, index, array) => {
        console.log(item, index, array)
      })

      // 在数组原型上添加 myMap 方法
      Array.prototype.myMap = function (callback) {
        let maparr = []
        for (let i = 0; i < this.length; i++) {
          maparr.push(callback(this[i], i, this))
        }
        return maparr
      }
      const result = arr.myMap((item, index, array) => {
        return item
      })
      console.log(result)

      // 在数组原型上添加 myFilter方法
      Array.prototype.myFilter = function (callback) {
        let filterArr = []
        for (let i = 0; i < this.length; i++) {
          if (callback(this[i], i, this)) {
            filterArr.push(this[i])
          }
        }
        return filterArr
      }
      const res = arr.myFilter((item, index, array) => item > 2)
      console.log(res)

      // 在数组原型上添加 myEvery 方法
      Array.prototype.MyEvery = function (callback) {
        for (let i = 0; i < this.length; i++) {
          if (!callback(this[i], i, this)) return false
        }
        return true
      }
      const res1 = arr.MyEvery((item) => {
        return item > 1
      })
      console.log(res1)
    </script>
  </body>
</html>
